package cn.edu.ctbu.datajpacrud.webapi;

import cn.edu.ctbu.datajpacrud.core.PageUtils;
import cn.edu.ctbu.datajpacrud.domain.Teacher;
import cn.edu.ctbu.datajpacrud.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/teacher")
public class TeacherRestController {

    @Autowired
    private TeacherService teacherService;



    /**
     * 读取的url:/webapi/teacher/list
     *
     * @return
     */
    @GetMapping("/list")
    public List<Teacher> getAll() {

        List<Teacher> teachers = teacherService.findAll();
        return teachers;
    }

    /**
     * 新增学生地方法./webapi/teacher/insert
     *
     * @param teacher
     * @return
     */
    @PostMapping("/insert")
    public Teacher insert(Teacher teacher) {
        Teacher teacher1 = teacherService.insert(teacher);
        return teacher1;
    }

    @PutMapping("/update")
    public Teacher update(Teacher teacher) {

        teacherService.update(teacher);
        return teacher;
    }

    @DeleteMapping("/delete/{id}")
    public void delete(Teacher teacher) {
        teacherService.delete(teacher);
    }

    @GetMapping("/get/{id}")
    public Teacher get(@PathVariable Integer id) {

        Teacher teacher = teacherService.getById(id);


        return teacher;
    }
    /**
     * 分页读取全部内容
     *
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "Page", defaultValue = "0") Integer page,
                               @RequestParam(value = "size", defaultValue = "10") Integer size,
                               @RequestParam(value = "name", defaultValue = "") String name,
                               @RequestParam(value = "sex",  defaultValue = "") String sex,
                               @RequestParam(value = "no",  defaultValue = "") Integer no
    ) {

        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<Teacher> teacherPage;


/**
 * 按no查询
 */
        if(StringUtils.isEmpty(no)){
            Pageable pageable = PageRequest.of(page,size,sort);
            teacherPage = teacherService.findAll(pageable);
        }else{
            Teacher student=new Teacher();
            student.setNo(no);
            Pageable pageable= PageRequest.of(0,10,sort);

            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Teacher> example = Example.of(student,matcher);

            teacherPage = teacherService.findAll(example,pageable);
        }
//        if (StringUtils.isEmpty(sex)) {
//            Pageable pageable = PageRequest.of(page, size, sort);
//            teacherPage = teacherService.findAll(pageable);
//        } else {
//            Teacher teacher = new Teacher();
//            teacher.setSex(sex);
//            Pageable pageable = PageRequest.of(0, 10, sort);
////            ExampleMatcher matcher = ExampleMatcher.matching()
////                    .withMatcher("sex", ExampleMatcher.GenericPropertyMatchers.contains());
////            Example<Teacher> example = Example.of(teacher, matcher);
//            Example<Teacher> example =Example.of(teacher);
//            teacherPage = teacherService.findAll(example, pageable);
//        }
//        if (StringUtils.isEmpty(name)) {
//            Pageable pageable = PageRequest.of(page, size, sort);
//            teacherPage = teacherService.findAll(pageable);
//        } else {
//            Teacher teacher = new Teacher();
//            teacher.setName(name);
//            Pageable pageable = PageRequest.of(0, 10, sort);
//            ExampleMatcher matcher = ExampleMatcher.matching()
//                    .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
//            Example<Teacher> example = Example.of(teacher, matcher);
//
//            teacherPage = teacherService.findAll(example, pageable);
//        }



////        Pageable pageable= PageRequest.of(page,size, sort);
////
////        teacherPage = teacherService.findAll(pageable);
//
        PageUtils pageUtils=new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());

        return pageUtils;
        }

}
